package session.stateless;

import entity.AdvertisingEntity;
import entity.MetaCategoryEntity;
import entity.MetaCuisineEntity;
import entity.MetaFoodPlaceEntity;
import entity.StakeholderEntity;
import exception.ExistException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.jboss.weld.logging.Category;
import util.enumeration.ActivityType;
import util.enumeration.CuisineType;
import util.enumeration.FnbType;

/**
 *
 * @author KohLi
 */
@Stateless
@LocalBean
public class AdvertisementSessionBean {

    @PersistenceContext
    private EntityManager entityManager;
    @EJB
    private StakeholderSessionBeanStateless stakeholderSession;

    public boolean addAdvertisement(AdvertisingEntity ads, Long stakeholderId, List<ActivityType> activityTypeList, List<CuisineType> cuisineTypeList, List<FnbType> fnbTypeList) throws ExistException {
        boolean successful = true;
        AdvertisingEntity advertisement = new AdvertisingEntity(ads);

        //get stakeholder entity 
        //StakeholderEntity stakeholder = new StakeholderEntity();

        System.out.println("managed to get the whole stakeholder entity;");
        //persist the stakeholder
        try {
            advertisement.setStakeholder(stakeholderSession.getStakeholderById(stakeholderId));

            // auto get system time for registration date
            Calendar cal = Calendar.getInstance();
            Date creationDate = cal.getTime();

            advertisement.setCreationDate(creationDate);

            advertisement.setActivityType(ads.getActivityType());
            advertisement.setCuisineTypeList(ads.getCuisineTypeList());
            advertisement.setFnBTypeList(ads.getFnBTypeList());

            entityManager.persist(advertisement);
            for (int i = 0; i < activityTypeList.size(); i++) {
                MetaCategoryEntity category = new MetaCategoryEntity();
                category.setActivityCategory(activityTypeList.get(i));
                System.out.println("category" + category.getActivityCategory());
                category.setAdvertisement(advertisement);
                entityManager.persist(category);
                advertisement.getActivityType().add(category);
            }

            for (int i = 0; i < cuisineTypeList.size(); i++) {
                MetaCuisineEntity cuisine = new MetaCuisineEntity();
                cuisine.setCusine(cuisineTypeList.get(i));
                System.out.println("cuisine" + cuisine.getCusine());
                cuisine.setAdvertisement(advertisement);
                entityManager.persist(cuisine);
                advertisement.getCuisineTypeList().add(cuisine);
            }
            MetaFoodPlaceEntity suitableFor = new MetaFoodPlaceEntity();
            for (int i = 0; i < fnbTypeList.size(); i++) {
                suitableFor.setSuitableFor(fnbTypeList.get(i));
                System.out.println("suitableFor" + suitableFor.getSuitableFor());
                suitableFor.setAdvertisement(advertisement);
                entityManager.persist(suitableFor);
                advertisement.getFnBTypeList().add(suitableFor);
            }
        } catch (Exception ex) {
            successful = false;
        }
        return successful;

    }

    public List<AdvertisingEntity> getAllAdvertisementByStakeholder(Long stakeholderId) {
        List<AdvertisingEntity> advertisementList = new ArrayList<AdvertisingEntity>();
        StakeholderEntity stakeholder = entityManager.find(StakeholderEntity.class, stakeholderId);
        if (stakeholder == null) {
            System.out.println("Stakeholder is null!");
            return advertisementList;
        } else {
            Query query = entityManager.createQuery("SELECT ads FROM AdvertisingEntity ads WHERE ads.stakeholder.id =:stakeholderId");
            query.setParameter("stakeholderId", stakeholderId);
            advertisementList = query.getResultList();
        }
        System.out.println("check advetisement size" + advertisementList.size());

        return advertisementList;


    }

    public boolean saveEditAds(AdvertisingEntity ads, Long adsId, List<ActivityType> activityTypeList, List<CuisineType> cuisineTypeList, List<FnbType> fnbTypeList) {
        boolean success = true;
        try {
            AdvertisingEntity advertisement = entityManager.find(AdvertisingEntity.class, adsId);
            advertisement.setAdName(ads.getAdName());
            advertisement.setPromotionDesc(ads.getPromotionDesc());
            advertisement.setStartDate(ads.getStartDate());
            advertisement.setEndDate(ads.getEndDate());

            Calendar cal = Calendar.getInstance();
            Date editDate = cal.getTime();
            advertisement.setLastEditDate(editDate);

            entityManager.persist(advertisement);
            advertisement.getCuisineTypeList().clear();
            advertisement.getActivityType().clear();
            advertisement.getFnBTypeList().clear();


            if (!activityTypeList.isEmpty()) {
                //getting the new category tagging
                for (int i = 0; i < activityTypeList.size(); i++) {
                    MetaCategoryEntity cat = new MetaCategoryEntity();
                    cat.setActivityCategory(activityTypeList.get(i));
                    System.out.println("category: " + cat.getActivityCategory());
                    entityManager.persist(cat);
                    advertisement.getActivityType().add(cat);
                }
            }
            if (!fnbTypeList.isEmpty()) {
                //getting the new suitable for tagging
                for (int i = 0; i < fnbTypeList.size(); i++) {
                    MetaFoodPlaceEntity suitable = new MetaFoodPlaceEntity();
                    suitable.setSuitableFor(fnbTypeList.get(i));
                    System.out.println("category: " + suitable.getSuitableFor());
                    entityManager.persist(suitable);
                    advertisement.getFnBTypeList().add(suitable);
                }
            }
            if (!cuisineTypeList.isEmpty()) {
                //getting the new cuisine tagging
                for (int i = 0; i < cuisineTypeList.size(); i++) {
                    MetaCuisineEntity cuisine = new MetaCuisineEntity();
                    cuisine.setCusine(cuisineTypeList.get(i));

                    System.out.println("cuisine" + cuisine.getCusine());
                    cuisine.setAdvertisement(advertisement);
                    entityManager.persist(cuisine);
                    advertisement.getCuisineTypeList().add(cuisine);
                }
            }




        } catch (Exception ex) {
            success = false;
        }

        return success;
    }

    private void checkAdvertisementOutdate() {
        Calendar cal = Calendar.getInstance();
        Date editDate = cal.getTime();

    }

    public boolean deleteAds(Long adLong) {
        boolean success = true;


        try {
            AdvertisingEntity ads = entityManager.find(AdvertisingEntity.class, adLong);
            entityManager.remove(ads);
        } catch (Exception ex) {
            success = false;
        }

        return success;


    }

    public AdvertisingEntity getAdsById(Long adId) {
        AdvertisingEntity ads = entityManager.find(AdvertisingEntity.class, adId);

        return ads;
    }

    /**
     * @return the entityManager
     */
    public EntityManager getEntityManager() {
        return entityManager;
    }

    /**
     * @param entityManager the entityManager to set
     */
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
}
